﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_TinMuaXe_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindKhoangGia()
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidId.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub BindKhoangGia()
        Using data As New VSEntities
            Dim p = (From q In data.TinMuaXeKhoangGias Select q).ToList
            Dim itm As New ListItem("--- Chọn ---", "0")
            With ddlTinmuaxekhoanggiaid
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataSource = p
                .DataBind()
                .Items.Insert(0, itm)
            End With
            'ddlKhuVuc

            ddlKhuVuc.AppendDataBoundItems = True
            ddlKhuVuc.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim ba = (From ca In data.DonViHanhChinhs Select ca.Id, ca.Title).ToList
            ddlKhuVuc.DataTextField = "Title"
            ddlKhuVuc.DataValueField = "ID"
            ddlKhuVuc.DataSource = ba
            ddlKhuVuc.DataBind()

        End Using
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Tinmuaxe = (From q In data.Tinmuaxes Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtTitle.Text = IIf(IsNothing(p.TieuDe) = True, "", p.TieuDe)
                txtDescription.Text = IIf(IsNothing(p.NoiDung) = True, "", p.TieuDe)

               
                ddlTinmuaxekhoanggiaid.SelectedValue = IIf(IsNothing(p.TinMuaXeKhoangGiaId) = True, "", p.TinMuaXeKhoangGiaId)
                txtHoTen.Text = p.LienHe
                txtDiaChi.Text = p.DiaChi
                txtDienThoai.Text = p.SoDienThoai
                ddlKhuVuc.SelectedValue = IIf(IsNothing(p.TinhThanhId) = True, 0, p.TinhThanhId)

            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Tinmuaxe = (From q In data.Tinmuaxes Where q.Id = hidID.Value).SingleOrDefault
            Try

                p.TieuDe = txtTitle.Text.Trim()
                p.NoiDung = txtDescription.Text.Trim()
                p.TinMuaXeKhoangGiaId = ddlTinmuaxekhoanggiaid.SelectedValue


                p.Modifier = Session("UserName")
                p.Modified = Now

                p.LienHe = txtHoTen.Text
                p.SoDienThoai = txtDienThoai.Text
                p.DiaChi = txtDiaChi.Text
                p.TinhThanhId = ddlKhuVuc.SelectedValue


                data.SaveChanges()

                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Tinmuaxe/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
